from api import log
from config import HOST

#注册登录api
class ApiRegisterLogin:
    # 初始化
    def __init__(self, session):
        # 获取session对象
        self.session = session
        # 1、图片验证码URL
        self.__url_img_code = HOST + "/common/public/verifycode1/{}"
        # 2、短信验证码URL
        self.__url_phone_code = HOST + "/member/public/sendSms"
        # 3、注册URL
        self.__url_register = HOST + "/member/public/reg"
        # 4、登录URL
        self.__url_login = HOST + "/member/public/login"
        # 5、登录状态URL
        self.__url_login_status = HOST + "/member/public/islogin"

    # 1、获取图片验证码接口 封装
    def api_img_code(self, random):
        log.info("正在调用 图片验证码 ,请求方法:{} ,url:{}, 参数:{}".format("get",self.__url_img_code,random))
        return self.session.get(url=self.__url_img_code.format(random))

    # 2、获取短信验证码接口 封装
    def api_phone_code(self, phone, imgVerifyCode):
        data = {
            "phone": phone,
            "imgVerifyCode": imgVerifyCode,
            "type": "reg"
        }
        log.info("正在调用 短信验证码 ,请求方法:{} ,url:{}, 参数:{}".format("post",self.__url_phone_code,data))
        return self.session.post(url=self.__url_phone_code, data=data)

    # 3、注册接口 封装
    def api_register(self, phone, password, verifycode, phone_code, invite_phone=None):
        data = {
            "phone": phone,
            "password": password,
            "verifycode": verifycode,
            "phone_code": phone_code,
            "dy_server": "on",
            "invite_phone": invite_phone
        }
        log.info("正在调用 注册 ,请求方法:{} ,url:{}, 参数:{}".format("post", self.__url_register, data))
        return self.session.post(url=self.__url_register, data=data)

    # 4、登录接口 封装
    def api_login(self, keywords="13600001111", password="test123"):
        data = {
            "keywords": keywords,
            "password": password
        }
        log.info("正在调用 登录 ,请求方法:{} ,url:{}, 参数:{}".format("post", self.__url_login, data))
        return self.session.post(url=self.__url_login, data=data)

    # 5、查询登录状态 封装
    def api_login_status(self):
        log.info("正在调用 查询登录状态 ,请求方法:{} ,url:{}".format("post", self.__url_login_status))
        return self.session.post(url=self.__url_login_status)




